﻿using blackhouse.BlackApart.Business.Apartments;
using blackhouse.BlackApart.WebNemo;
using blackhouse.mojoPortalExtend;
using blackhouse.mojoPortalExtend.Urls;
using blackhouse.WebNemo.Business.Groups;
using mojoPortal.Business;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace blackhouse.StareKino.Web {
    public class ApartmentTypeUrlSupport : UrlSupport {

        #region Fields

        private IApartmentGroup apartmentType;
        private NemoApartmentType nemoApartmentType;
        private int urlVariantIdx = 0;

        #endregion

        #region Constructors

        public ApartmentTypeUrlSupport(IApartmentGroup apartmentType) {
            this.apartmentType = apartmentType;
            this.nemoApartmentType = this.apartmentType as NemoApartmentType;

            this.OldUrl = this.GetOldUrl();
            this.NewUrl = this.GetNewUrl(this.urlVariantIdx);
            string realUrl = "~/Apartment.aspx?id=" + this.apartmentType.Id;
            this.Guid = this.apartmentType.Guid;
            PageProvider page = new PageProvider();
            PageSettings apartmentsPage = page.FindCmsPageByFunctionality("apartments");
            if (apartmentsPage != null)
                realUrl = realUrl + "&pageId=" + apartmentsPage.PageId;
            this.RealUrl = realUrl;
        }

        #endregion

        #region Methods

        private string GetOldUrl() {
            if (this.nemoApartmentType != null && this.nemoApartmentType.UrlId > 0) {
                FriendlyUrl fUrl = new FriendlyUrl(this.nemoApartmentType.UrlId);
                if (fUrl.FoundFriendlyUrl)
                    return "~/" + fUrl.Url;
            }
            return this.apartmentType.Url;
        }

        private string GetNewUrl(int variantIdx) {
            string title = this.apartmentType.Name;
            if (string.IsNullOrEmpty(title))
                title = this.apartmentType.ShortName;
            title = title.RemovePolishChars().CleanStringForUrl();
            switch (variantIdx) {
                case 0:
                    return "~/" + title;
                case 1:
                    return "~/" + HttpContext.GetGlobalResourceObject("System", "apartmentUrl") + "-" + title;
                default:
                    throw new UrlInUseException(this);
            }
        }

        public override void ChangeToOtherNewUrl() {
            if (this.NewUrl.Equals(GetNewUrl(this.urlVariantIdx), StringComparison.InvariantCultureIgnoreCase))
                this.urlVariantIdx++;
            this.NewUrl = GetNewUrl(this.urlVariantIdx);
        }

        public override void Save() {
            this.apartmentType.Url = this.NewUrl;
            this.nemoApartmentType.UrlId = this.FriendlyUrl.UrlId;
            this.nemoApartmentType.Save(false);
        }

        public void SetUserNewUrl(string url) {
            this.NewUrl = url;
        }

        #endregion

    }
}
